﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

using System.IO;

public partial class DataTableMan : System.Web.UI.Page
{
    string userName = null;
    DBOperation db = new DBOperation();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userName"] == null || Session["roleID"] == null)
        {
            Response.Redirect("~/Login.aspx");
            Response.End();
        }
        userName = Session["userName"].ToString();
        if (!IsPostBack)
        {
            
            IsAdmin();
        }

    }

    //判断是否是admin，如果是，给予初始化权利
    protected void IsAdmin()
    {
        if (userName == "admin")
            btnClear.Enabled = true;

    }
    /// <summary>
    /// 密码认证
    /// </summary>
    /// <param name="pwd">用户密码</param>
    /// <returns>通过返回真</returns>
    protected bool checkuser(string pwd)
    {
        return Validated.validatedUser(userName, pwd);
    }
    /// <summary>
    /// 删除服务器上files文件夹里的内容
    /// </summary>
    private void deletefile(System.IO.DirectoryInfo path)
    {
        foreach (System.IO.DirectoryInfo d in path.GetDirectories())
        {
            deletefile(d);
        }
        foreach (System.IO.FileInfo f in path.GetFiles())
        {
            f.Delete();
        }
    }

    protected void btnClear_Click(object sender, EventArgs e)
    {
        if (checkuser(txtPwd.Text.Trim()))
        {
            lblMsg.Visible = false;
            if (rbtnNoSaveUser.Checked)
            {
                ArrayList sqllist = new ArrayList();
                string[] tableName ={ "commFiles", "DATABACKUP","DATAYEAR", "DEPARTMENT", "FLOWSUBTYPE", "FLOWTYPE", "LCBZMINFO", "LCMINFO", "LOG", "NEWS", "ROLE", "USERS" };
                for (int i = 0; i < tableName.Length; i++)
                {
                    string sql = "truncate table " + tableName[i];
                    sqllist.Add(sql);
                }

                string currentYear = Convert.ToString(DateTime.Now.Year);
                string[] tableNameYear ={ "DATA"+currentYear, "LC" + currentYear, "LCBZ" + currentYear, "MESSAGE" + currentYear, "YCFILES" + currentYear, "YCMS" + currentYear, "YCSTEPS" + currentYear, "ZBZL" + currentYear, "ATTACHFILES" + currentYear };
                for (int i = 0; i < tableNameYear.Length; i++)
                {
                    string sql = "truncate table " + tableNameYear[i];
                    sqllist.Add(sql);
                }

                if (db.ExcuteSqls(sqllist))
                {
                    ArrayList insertSql = new ArrayList();
                    insertSql.Add("insert into DEPARTMENT(DEPNAME,PRINCIPALID,STATUS,BRIEF) values('生技部',2,1,'系统内置的最高管理部门') ");
                    insertSql.Add("insert into DEPARTMENT(DEPNAME,PRINCIPALID,STATUS,BRIEF) values('营销部',3,1,'系统内置的职能管理部门') ");
                    insertSql.Add("insert into DEPARTMENT(DEPNAME,PRINCIPALID,STATUS,BRIEF) values('调度管理所',4,1,'系统内置的职能管理部门') ");
                    insertSql.Add("insert into DEPARTMENT(DEPNAME,PRINCIPALID,STATUS,BRIEF) values('输配电管理所',5,1,'系统内置的职能管理部门') ");
                    insertSql.Add("insert into DEPARTMENT(DEPNAME,PRINCIPALID,STATUS,BRIEF) values('客户服务中心',6,1,'系统内置的职能管理部门') ");
                    insertSql.Add("insert into DEPARTMENT(DEPNAME,PRINCIPALID,STATUS,BRIEF) values('计量中心',7,1,'系统内置的职能管理部门') ");
                    insertSql.Add("insert into DEPARTMENT(DEPNAME,PRINCIPALID,STATUS,BRIEF) values('变电管理所',8,1,'系统内置的职能管理部门') ");

                    insertSql.Add("insert into ROLE(ROLENAME,ACCESSRIGHT)values('系统管理员','系统最高权限管理员')");
                    insertSql.Add(" insert into ROLE(ROLENAME,ACCESSRIGHT)values('职能用户','系统高级管理员')");
                    insertSql.Add("insert into ROLE(ROLENAME,ACCESSRIGHT)values('一般用户','系统一般用户')");


                    insertSql.Add("insert into DATAYEAR(DATAYEAR) values ('"+currentYear+"')");

                    insertSql.Add("insert into USERS(USERNAME,TRUENAME,PASSWD,DEPID,ROLEID)values('admin','管理员','202cb962ac59075b964b07152d234b70',1,1)");
                    insertSql.Add("insert into USERS(USERNAME,TRUENAME,PASSWD,DEPID,ROLEID)values('huang_p','黄频','202cb962ac59075b964b07152d234b70',1,1)");
                    insertSql.Add("insert into USERS(USERNAME,TRUENAME,PASSWD,DEPID,ROLEID)values('wan_yq','万业群','202cb962ac59075b964b07152d234b70',2,2)");
                    insertSql.Add("insert into USERS(USERNAME,TRUENAME,PASSWD,DEPID,ROLEID)values('meng_jl','蒙剑玲','202cb962ac59075b964b07152d234b70',3,2)");
                    insertSql.Add("insert into USERS(USERNAME,TRUENAME,PASSWD,DEPID,ROLEID)values('wang_q','王勤','202cb962ac59075b964b07152d234b70',4,2)");
                    insertSql.Add("insert into USERS(USERNAME,TRUENAME,PASSWD,DEPID,ROLEID)values('chen_w','陈伟','202cb962ac59075b964b07152d234b70',5,2)");
                    insertSql.Add("insert into USERS(USERNAME,TRUENAME,PASSWD,DEPID,ROLEID)values('zhu_qr','朱启荣','202cb962ac59075b964b07152d234b70',6,2)");
                    insertSql.Add("insert into USERS(USERNAME,TRUENAME,PASSWD,DEPID,ROLEID)values('jiang_jl','蒋继林','202cb962ac59075b964b07152d234b70',7,2)");
                    insertSql.Add("insert into USERS(USERNAME,TRUENAME,PASSWD,DEPID,ROLEID)values('zhong_w','钟巍','202cb962ac59075b964b07152d234b70',6,2)");
                    insertSql.Add("insert into USERS(USERNAME,TRUENAME,PASSWD,DEPID,ROLEID)values('yang_hw','杨华文','202cb962ac59075b964b07152d234b70',5,2)");

                    insertSql.Add("insert into FLOWTYPE (TYPENAME,SHORTMARK,TABLEPREFIX,STATUS) values ('基础资料','JC','JCZL','1')");
                    insertSql.Add("insert into FLOWTYPE (TYPENAME,SHORTMARK,TABLEPREFIX,STATUS) values ('分析报告','FX','FXBG','1')");
                    insertSql.Add("insert into FLOWTYPE (TYPENAME,SHORTMARK,TABLEPREFIX,STATUS) values ('计量抄表','JL','JLCB','1')");
                    insertSql.Add("insert into FLOWTYPE (TYPENAME,SHORTMARK,TABLEPREFIX,STATUS) values ('考核资料','KH','KHZL','1')");
                    insertSql.Add("insert into FLOWTYPE (TYPENAME,SHORTMARK,TABLEPREFIX,STATUS) values ('分线、分台区完成资料','FW','FFWC','1')");
                    insertSql.Add("insert into FLOWTYPE (TYPENAME,SHORTMARK,TABLEPREFIX,STATUS) values ('理论计算','LJ','LLJS','1')");
                    insertSql.Add("insert into FLOWTYPE (TYPENAME,SHORTMARK,TABLEPREFIX,STATUS) values ('指标完成','ZW','ZBWC','1')");

                    if (db.ExcuteSqls(insertSql))
                    {
                        DirectoryInfo path = new DirectoryInfo(Server.MapPath("~/files"));
                        deletefile(path);
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "", "alert('初始化成功!');", true);  

                    }
                    else
                    {
                        Response.Write("<script language='javascript'>alert('在尝试读写数据库时，发送未知异常，初始化失败！')</script>");
                    }
                }
            }
            //保留用户、部门 、角色
            if (rbtnSaveUser.Checked)
            {
                lblMsg.Visible = false;

                ArrayList sqllist = new ArrayList();
                string[] tableName = { "commFiles", "DATABACKUP", "DATAYEAR", "FLOWSUBTYPE", "FLOWTYPE", "LCBZMINFO", "LCMINFO", "LOG", "NEWS" };
                for (int i = 0; i < tableName.Length; i++)
                {
                    string sql = "truncate table " + tableName[i];
                    sqllist.Add(sql);
                }

                string currentYear = Convert.ToString(DateTime.Now.Year);
                string[] tableNameYear = {"DATA" + currentYear, "LC" + currentYear, "LCBZ" + currentYear, "MESSAGE" + currentYear, "YCFILES" + currentYear, "YCMS" + currentYear, "YCSTEPS" + currentYear,"ZBZL" + currentYear, "ATTACHFILES" + currentYear };
                for (int i = 0; i < tableNameYear.Length; i++)
                {
                    string sql = "truncate table " + tableNameYear[i];
                    sqllist.Add(sql);
                }

                if (db.ExcuteSqls(sqllist))
                {
                    ArrayList insertSql = new ArrayList();
                    insertSql.Add("insert into FLOWTYPE (TYPENAME,SHORTMARK,TABLEPREFIX,STATUS) values ('基础资料','JC','JCZL','1')");
                    insertSql.Add("insert into FLOWTYPE (TYPENAME,SHORTMARK,TABLEPREFIX,STATUS) values ('指标资料','ZB','ZBZL','1')");
                    insertSql.Add("insert into FLOWTYPE (TYPENAME,SHORTMARK,TABLEPREFIX,STATUS) values ('指标计划','ZP','ZBPL','1')");
                    insertSql.Add("insert into DATAYEAR(DATAYEAR) values ('" + currentYear + "')");

                    if (db.ExcuteSqls(insertSql))
                    {
                        DirectoryInfo path = new DirectoryInfo(Server.MapPath("~/files"));
                        deletefile(path);

                        Response.Write("<script language='javascript'>alert('初始化成功！')</script>");

                    }
                    else
                    {
                        Response.Write("<script language='javascript'>alert('在尝试读写数据库时，发送未知异常，初始化失败！')</script>");
                    }
                }

            }
        }
        else
        {
            lblMsg.Visible = true;
            lblMsg.Text = "<font color='red'>密码错误！</font>";
        }
    }
}
